/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.songlanyun.modules.account.controller.common;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.modules.account.entity.AccountEntity;
import com.songlanyun.modules.exception.MemberException;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.shop.entity.ShopEntity;
import com.songlanyun.modules.shop.service.ShopService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Controller公共组件
 *
 * @author Mark sunlightcs@gmail.com
 */
public abstract class AbstractApiController {

    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected RedisTemplate<String, String> redisTemplate;
    @Autowired
    protected AccountService accountService;
    @Autowired
    protected ShopService shopService;

    /**
     * 获取会员id
     *
     * @return
     */
    protected Long getUserId(boolean isThrow) {
        //isThrow 是否抛异常，不抛的话返回常量0
        String api_uid = (String) request.getAttribute("api_uid");

        if (StringUtils.isBlank(api_uid)) {
            if (isThrow) {
                throw new RRException(MemberException.NOT_LOGIN);
            }
            return 0L;
        } else {
            Long uid = Long.parseLong(api_uid);
            AccountEntity account = accountService.getById(uid);

            if (account == null) {
                //会员不存在
                if (isThrow) {
                    throw new RRException(MemberException.USER_EXCEPTION);
                }
                return 0L;
            }
            if (account.getLockStatus()) {
                //会员被锁定
                if (isThrow) {
                    throw new RRException(MemberException.USER_DISABLED);
                }
                return 0L;
            }

            return uid;
        }


    }

    /**
     * 获取当前会员
     *
     * @return
     */
    protected AccountEntity getMember() {

        return accountService.getUserById(getUserId(false), true);

    }


    /**
     * 根据用户id获取会员隶属商铺id
     *
     * @return
     */
    protected Long getSubShopById(Long userId) {
        ShopEntity shop = shopService.getSubShopById(userId, false);

        return shop.getId();
    }

    /**
     * 根据用户id获取会员商铺id
     *
     * @return
     */
    protected Long getShopByUserId(Long userId, boolean unionBusiness, boolean isThrow) {
        ShopEntity shop = shopService.getShopByUserId(userId, unionBusiness);
        if (ObjectUtil.isNotNull(shop)) {
            return shop.getId();
        } else {
            if (isThrow) {
                throw new RRException(MemberException.ACCOUNT_NUMBER_NOT_UNION_BUSINESS);
            }
        }
        return null;
    }

    /**
     * 获取客户端的值（client=app 或 applet）
     *
     * @return
     */
    protected String getClient() {
        String client = request.getHeader("client");
        if (StringUtils.isBlank(client)) {
            client = (String) request.getAttribute("client");
        }
        if (StringUtils.isBlank(client)) {
            client = "applet";
        }

        return client;
    }

    /**
     * 是否是小程序
     *
     * @return
     */
    protected boolean isApplet() {
        return "applet".equals(this.getClient());
    }

    /**
     * 是否是APP
     *
     * @return
     */
    protected boolean isApp() {
        return "app".equals(this.getClient());
    }

    /**
     * 用户锁定
     *
     * @param LockStatus
     */
    protected void isLock(boolean LockStatus) {
        if (!LockStatus) {
            throw new RRException(MemberException.USER_DISABLED);
        }
    }

    protected Long setUserId(Map<String, Object> params) {
        //如果传入userId，则代表是他人查看我的主页，反之是自己查询我的主页
        Long userId = MapUtil.getLong(params, "userId");
        if (userId == null) {
            userId = getUserId(true);
            params.put("userId", userId);
        }

        return userId;
    }
}
